package com.cskaoyan.market.shiro;

import com.cskaoyan.market.service.wx.WxAuthService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName WxRealm
 * @Description
 * @Author MZ
 * @Date 2023/5/19 18:25
 */
@Component
public class WxRealm extends AuthorizingRealm {

    @Autowired
    WxAuthService wxAuthService;

    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // 用户名密码认证
        CskaoyanToken cskaoyanToken = (CskaoyanToken) token;
        String username = cskaoyanToken.getUsername();
        String password = wxAuthService.checkPasswordByUsername(username);

        return new SimpleAuthenticationInfo(username, password, getName());
    }

    // 授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        String username = (String) principals.getPrimaryPrincipal();
//        //获取其授权信息
//        List<String> perms = adminService.selectPermissionsByUsername(username);
//        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //此处，我们在鉴权时，只通过权限来进行鉴定，而不通过角色了
//        authorizationInfo.addStringPermissions(perms);
        return null;
    }


}
